﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SIS.BLL;
using SIS.ENT;

public partial class Teacher_FamilyMember_Detail : System.Web.UI.Page
{
    SIS.UI.Common _common = new SIS.UI.Common();

    #region  "Events"

        protected void Page_Load(object sender, EventArgs e)
        {
            lblTopMessage.Text = DateTime.Now.ToString();
            if (!Page.IsPostBack)
            {
                SIS.Utilities.General.LoadDayDropdownList(ddlDOBDay);
                SIS.Utilities.General.LoadMonthDropdownList(ddlDOBMonth, 2);
                SIS.Utilities.General.LoadYearDropdownList(ddlDOBYear, (DateTime.Now.Year - 120), DateTime.Now.Year, 1);            
            
                _common.LoadBloodGroupDDL(ddlBloodGroup, string.Empty);
                _common.LoadMartialStatusCodeDDL(ddlMaritalStatus, string.Empty);
                _common.LoadRaceCodeDDL(ddlRaceCode, string.Empty);
                _common.LoadReligionCodeDDL(ddlReligion, string.Empty);
                _common.LoadNationalityDDL(ddlNationality, string.Empty);
                _common.LoadTravelDocDDL(ddlTravelDocType, string.Empty);
                _common.LoadDrivingLicenseTypeDDL(ddlDrivingLicenceType, string.Empty);
                _common.LoadRelationshipTypeDDL(ddlGSRelation, string.Empty);           
                _common.LoadCountryCodeDDL(ddlSRCountry, string.Empty);
                _common.LoadCountryCodeDDL(ddlCountry, string.Empty);
           

                if (Request.Params["id"] != null)
                {

                    int _id = -1;
                    if (Int32.TryParse(Request.Params["id"].ToString(), out _id))
                    {
                        if (LoadTeacherInfo(_id))
                        {
                            LoadFamilyMemberList(_id);
                            grvTeacherProfile.Visible = true;
                            lblTeacherUserAccountKey.Text = _id.ToString();                        
                            divTeacherProfile.Visible = false;
                            divTeacherDetails.Visible = true;
                            hplTeacher.NavigateUrl = "~/Teacher/TeacherEntry.aspx?id=" + _id.ToString();
                            lblTopMessage.Text= "Add or Edit Family Details - " + DateTime.Now.ToString();
                        
                        }
                        else
                        {
                            divTeacherDetails.Visible = false;
                            divTeacherProfile.Visible = false;
                            btnSubmit.Enabled = false;
                            btnNew.Enabled = false;
                            tbcEntry.Enabled = false;
                        }
                    }
                    else
                    {
                        lblTopMessage.Text = "Invalid guardian account ID.";
                    }

               
                }
                else
                {
                    lblTopMessage.Text = "Add New Family - " + DateTime.Now.ToString();
                    divTeacherProfile.Visible = true;
                    btnSubmit.Enabled = false;
                    btnNew.Enabled = false;
                    tbcEntry.Enabled = false;
                }
            }
        }

        protected void ddlSRCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            _common.LoadDistrictCodeDDL(ddlSRDistrict, Convert.ToInt16(ddlSRCountry.Text), string.Empty);
        }

        protected void ddlSRDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            _common.LoadCityCodeDDL(ddlSRCity, Convert.ToInt32(ddlSRDistrict.Text), Convert.ToInt16(ddlSRCountry.Text), string.Empty);
        }

        protected void ddlSchoolList_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblSchoolKey.Text = ddlSchoolList.SelectedValue;
        }

        protected void btnShowTeacherProfile_Click(object sender, EventArgs e)
        {

        }

        protected void grvTeacherProfile_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (grvTeacherProfile.SelectedIndex >= 0)
            {
                DataKey _selectedDataKey = grvTeacherProfile.SelectedDataKey;
                int _TeacherAccountKey = Convert.ToInt32(_selectedDataKey[0]);

                if (_TeacherAccountKey > 0)
                {
                    lblTeacherUserAccountKey.Text = _TeacherAccountKey.ToString();
                    if (LoadTeacherInfo(_TeacherAccountKey))
                    {
                        LoadFamilyMemberList(_TeacherAccountKey);
                        divTeacherDetails.Visible = true;
                        hplTeacher.NavigateUrl = "~/Teacher/TeacherEntry.aspx?id=" + _TeacherAccountKey.ToString();
                        tbcEntry.Enabled = true;
                        btnNew.Enabled = true;
                        btnSubmit.Enabled = true;
                        lblTopMessage.Text = "Add new Family Member - " + DateTime.Now.ToString("dddd, MMMM dd, yyyy");
                    }
                    else
                    {
                        btnSubmit.Enabled = false;
                        btnNew.Enabled = false;
                        tbcEntry.Enabled = false;
                    }
                }
            }
        }

        protected void gvFamilyMemberList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (gvFamilyMemberList.SelectedIndex >= 0)
            {

                DataKey _selectedDataKey = gvFamilyMemberList.SelectedDataKey;
                int _gsRelationID = Convert.ToInt32(_selectedDataKey[0]);
                int _UserAccountKey = Convert.ToInt32(_selectedDataKey[1]);
                int _FamilyMemberProfileKey = Convert.ToInt32(_selectedDataKey[2]);
                int _FamilyMemberUserAccountKey = Convert.ToInt32(_selectedDataKey[3]);

                if (_UserAccountKey > 0 && _FamilyMemberProfileKey > 0)
                {
                    lblTeacherUserAccountKey.Text = _UserAccountKey.ToString();
                    lblFamilyMemberAccountKey.Text = _FamilyMemberProfileKey.ToString();
                    LoadFamilyMemberInfo(_gsRelationID, _FamilyMemberUserAccountKey);
                    lblTopMessage.Text = "Update Family detail - " + DateTime.Now.ToString("dddd, MMMM dd, yyyy");
                }
            }
        }

        protected void gvFamilyMemberList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = e.RowIndex;
            gvFamilyMemberList.SelectedIndex = index;
            int RelationID = Convert.ToInt32(gvFamilyMemberList.DataKeys[e.RowIndex][0]);
            int TeacherUserAccountKey = Convert.ToInt32(gvFamilyMemberList.DataKeys[e.RowIndex][1]);
            int FamilyMemberProfileKey = Convert.ToInt32(gvFamilyMemberList.DataKeys[e.RowIndex][2]);
            int FamilyMemberUserAccountKey = Convert.ToInt32(gvFamilyMemberList.DataKeys[e.RowIndex][3]);
            FamilyMemberProfileTable _objFamilyMember = FamilyMemberProfileTableManager.GetItem(FamilyMemberProfileKey);
            bool result = FamilyMemberProfileTableManager.Delete(_objFamilyMember);
            if (result == true)
            {
                lblTopMessage.Text = "Deleted was succeeded at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");
            }
            LoadFamilyMemberList(TeacherUserAccountKey);
            ClearFamilyMemberInfo();
        }

        protected void btnAddNewTeacher_Click(object sender, EventArgs e)
        {

            Response.Redirect("TeacherEntry.aspx");

        }

        protected void btnAddNewFamily_Click(object sender, EventArgs e)
        {
            if (lblTeacherUserAccountKey.Text != "-1")
            {
                Response.Redirect("FamilyMember-detail.aspx?id=" + lblTeacherUserAccountKey.Text);
            }
        }

        protected void btnViewTeacherList_Click(object sender, EventArgs e)
        {
            Response.Redirect("Teachers-List.aspx");
        }

        protected void ContactNumbers_ServerSideValidate(object source, ServerValidateEventArgs args)
        {
            bool _retValue = true;

            txtHomePhoneNumber.Text = txtHomePhoneNumber.Text.Trim();
            txtMobileNumber.Text = txtMobileNumber.Text.Trim();
            txtOtherContactNumber.Text = txtOtherContactNumber.Text.Trim();
            txtEmailAddress.Text = txtEmailAddress.Text.Trim();

            if (txtEmailAddress.Text.Length == 0 && txtHomePhoneNumber.Text.Length == 0 && txtMobileNumber.Text.Length == 0 && txtOtherContactNumber.Text.Length == 0)
            {
                lblMessage.Text += "Family Member's Contact Infomation are NOT sufficient!<br/>";
                _retValue = false;
            }
            else
            {

                _retValue = true;
            }

            args.IsValid = _retValue;
        }

        protected void DOB_ServerSideValidation(object source, ServerValidateEventArgs args)
        {
            bool _retValue = true;
            DateTime _dtDOB = DateTime.Now;
            string _strDOB = string.Empty;

            if (ddlDOBDay.Text != "-1" && ddlDOBMonth.Text != "-1" && ddlDOBYear.Text != "-1")
            {
                _strDOB = ddlDOBDay.Text + "/" + ddlDOBMonth.SelectedItem.Text + "/" + ddlDOBYear.Text.ToString();
                if (!SIS.Utilities.General.GetDateTime(_strDOB, out _dtDOB))
                {

                    lblMessage.Text += _strDOB + "  " + " Family Memeber's DOB is invalid!<br/>";
                    _retValue = false;
                }
                else
                {
                    _retValue = true;
                    //lblMessage.Text += "Student DOB: " + _dtDOB.ToString("dd/MMM/yyyy") + "<br/>";
                }
            }
            else
            {
                lblMessage.Text += "Select Guardian DOB.<br/>";
                _retValue = false;
            }
            args.IsValid = _retValue;
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            _common.LoadDistrictCodeDDL(ddlDistrict, Convert.ToInt16(ddlCountry.Text), string.Empty);

        }

        protected void ddlPCountry_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            _common.LoadCityCodeDDL(ddlCity, Convert.ToInt32(ddlDistrict.Text), Convert.ToInt16(ddlCountry.Text), string.Empty);
        }

        protected void ddlPDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "";

            bool _status = true;
            Page.Validate("FamilyMemberProfile");
            if (Page.IsValid && CheckValidation())
            {
                UserAccountTable _account = new UserAccountTable();
                _account.UserAccountKey = Convert.ToInt32(lblFamilyMemberAccountKey.Text);
                _account.SchoolKey = Convert.ToInt32(ddlSchoolList.Text);
                if (_account.UserAccountKey <= 0)
                {
                    _account.UserAccountTypeCode = 700;
                    _account.AuthCode = Guid.NewGuid().ToString().Replace("-", "").Substring(0, 6);
                    _account.AuthSalt = "GuardianGUID" + _account.AuthCode;

                    _account.UserAccountAlternateKey = txtFirstName.Text.Replace(" ", ".") + txtLastName.Text.Replace(" ", ".") + "@" + ddlSchoolList.Text;
                    _account.Status = -1;

                    UserAccountTableManager.Save(_account).ToString();

                    if (_account.UserAccountKey <= 0)
                    {
                        lblMessage.Text = "Saving Family Member User Account was failed!";
                        _status = false;
                        return;
                    }
                    else
                    {
                        lblFamilyMemberAccountKey.Text = _account.UserAccountKey.ToString();
                    }
                }

                FamilyMemberProfileTable _profile = new FamilyMemberProfileTable();

                FamilyMemberContactTable _contact = new FamilyMemberContactTable();

                FamilyMemberAddressTable _address = new FamilyMemberAddressTable();

                _profile.UserAccountKey = _account.UserAccountKey;
                _contact.UserAccountKey = _account.UserAccountKey;
                _address.UserAccountKey = _account.UserAccountKey;

                _profile.FamilyMemberProfileKey = Convert.ToInt32(lblFamilyMemberProfileKey.Text);
                _contact.FamilyMemberContactKey = Convert.ToInt32(lblFamilyMemberContactKey.Text);


                _profile.FirstName = txtFirstName.Text;
                _profile.LastName = txtLastName.Text;
                _profile.OtherName = txtOtherName.Text;

                DateTime _dtDOB = DateTime.Now;
                string _strDOB = string.Empty;
                _profile.DateOfBirth = new DateTime(1900, 1, 1);

                _profile.Gender = ddlGender.Text;
                _strDOB = ddlDOBDay.Text + "/" + ddlDOBMonth.SelectedItem.Text + "/" + ddlDOBYear.Text.ToString();
                if (SIS.Utilities.General.GetDateTime(_strDOB, out _dtDOB))
                {
                    _profile.DateOfBirth = _dtDOB;
                }
                else
                {
                    //lblMessage.Text += _strDOB + " " + "NO User DOB: " + _dtDOB.ToString("dd/MMM/yyyy") + "<br/>";
                }

                _profile.NRICNumber = txtNRICNumber.Text;

                _profile.DrivingLicenceType = Convert.ToInt16(ddlDrivingLicenceType.Text);
                _profile.DrivingLicenseNumber = txtDrivingLicenceNumber.Text;

                _profile.TravelDocumentType = Convert.ToInt16(ddlTravelDocType.Text);
                _profile.TravelDocumentNumber = txtTravelDocNumber.Text;

                _profile.BloodGroupCode = Convert.ToInt16(ddlBloodGroup.Text);
                _profile.MartialStatusCode = Convert.ToInt16(ddlMaritalStatus.Text);
                _profile.RaceCode = Convert.ToInt16(ddlRaceCode.Text);
                _profile.ReligionCode = Convert.ToInt16(ddlReligion.Text);
                _profile.NationalityCode = Convert.ToInt16(ddlNationality.Text);
                _profile.WebSiteUrl = txtWebsiteUrl.Text;
                _profile.CreatedDateTime = DateTime.Now;
                _profile.ModifiedDateTime = DateTime.Now;

                FamilyMemberProfileTableManager.Save(_profile);

                if (_profile.FamilyMemberProfileKey <= 0)
                {
                    lblMessage.Text += "Saving profile was failed!<br/>";
                    _status = false;

                }
                else
                {
                    lblFamilyMemberProfileKey.Text = _profile.FamilyMemberProfileKey.ToString();

                }
                _contact.EmailAddress = txtEmailAddress.Text;
                _contact.MobileNumber = txtMobileNumber.Text;
                _contact.HomePhoneNumber = txtHomePhoneNumber.Text;
                _contact.OtherNumber = txtOtherContactNumber.Text;
                _contact.FaxNumber = txtFaxNumber.Text;
                _contact.OfficePhoneNumber = txtOfficePhoneNumber.Text;
                _contact.Status = 1;
                _contact.CreatedDateTime = DateTime.Now;
                _contact.ModifiedDateTime = DateTime.Now;
                FamilyMemberContactTableManager.Save(_contact);


                if (_contact.FamilyMemberContactKey <= 0)
                {
                    lblMessage.Text += "Saving contact was failed!<br/>";
                    _status = false;
                    //lblStudentContactKey.Text = "-1";
                }
                else
                {
                    lblFamilyMemberContactKey.Text = _contact.FamilyMemberContactKey.ToString();
                }


                //Resident Address
                _address.FamilyMemberAddressKey = Convert.ToInt32(lblFamilyMemberPAddressKey.Text);

                if (chkSameAsResident.Checked)
                {
                    _address.UnitNo = txtSRUnitNo.Text;
                    _address.StreetName = txtSRStreetName.Text;
                    _address.CountryCode = Convert.ToInt16(ddlSRCountry.Text);
                    _address.DistrictCode = ddlSRDistrict.Text.Equals(string.Empty) ? Convert.ToInt16(-1) : Convert.ToInt16(ddlSRDistrict.Text);
                    _address.CityCode = ddlSRCity.Text.Equals(string.Empty) ? Convert.ToInt16(-1) : Convert.ToInt16(ddlSRCity.Text);
                    _address.PostalCode = txtSRPostCode.Text;
                }
                else
                {
                    _address.UnitNo = txtUnitNo.Text;
                    _address.StreetName = txtStreetName.Text;
                    _address.CountryCode = Convert.ToInt16(ddlCountry.Text);
                    _address.DistrictCode = ddlDistrict.Text.Equals(string.Empty) ? Convert.ToInt16(-1) : Convert.ToInt16(ddlDistrict.Text);
                    _address.CityCode = ddlCity.Text.Equals(string.Empty) ? Convert.ToInt16(-1) : Convert.ToInt16(ddlCity.Text);
                    _address.PostalCode = txtPostCode.Text;
                }

                _address.AddressTypeCode = 1;
                _address.Status = 1;
                _address.CreatedDateTime = DateTime.Now;
                _address.ModifiedDateTime = DateTime.Now;

                FamilyMemberAddressTableManager.Save(_address);

                if (_address.FamilyMemberAddressKey <= 0)
                {
                    lblMessage.Text += "Saving Resident address was failed!<br/>";
                    _status = false;
                    //lblStudentAddressKey.Text = "-1";
                }
                else
                {
                    lblFamilyMemberPAddressKey.Text = _address.FamilyMemberAddressKey.ToString();
                }

                FamilyMemberRelationshipTable _gsrelation = new FamilyMemberRelationshipTable();
                _gsrelation.RelationshipID = Convert.ToInt32(lblTeacherFamilyMemberRelationTypeKey.Text);

                _gsrelation.RelationshipTypeCode = Convert.ToInt16(ddlGSRelation.Text);
                _gsrelation.UserAccountKey = Convert.ToInt32(lblTeacherUserAccountKey.Text);
                _gsrelation.FamilyMemberProfileKey = Convert.ToInt32(lblFamilyMemberProfileKey.Text);
                _gsrelation.EmergencyContact = txtMobileNumber.Text.Trim().ToString().Length >= 0 ? txtMobileNumber.Text : txtHomePhoneNumber.Text;
                _gsrelation.Status = 1;
                _gsrelation.CreatedDateTime = DateTime.Now;
                _gsrelation.ModifiedDateTime = DateTime.Now;

                FamilyMemberRelationshipTableManager.Save(_gsrelation);

                if (_gsrelation.RelationshipID <= 0)
                {
                    lblMessage.Text = "Saving Teacher and Family Member Relation was failed!";
                    _status = false;
                    return;
                }
                else
                {
                    lblTeacherFamilyMemberRelationTypeKey.Text = _gsrelation.RelationshipID.ToString();
                }
                //}

                if (lblMessage.Text.Length == 0)
                {
                    lblTopMessage.Text = "Saving was succeeded at " + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");
                    lblMessage.Text = "Saving was succeeded at" + DateTime.Now.ToString("dd/MMM/yyyy hh:mm tt");
                }

                LoadFamilyMemberList(Convert.ToInt32(lblTeacherUserAccountKey.Text));
                //ClearFamilyMemberInfo();            
            }

        }

        protected void chkSameAsResident_CheckedChanged(object sender, EventArgs e)
        {

            if (lblTeacherUserAccountKey.Text == "-1")
            {
                chkSameAsResident.Checked = false;
                return;
            }

            if (chkSameAsResident.Checked)
            {
                txtUnitNo.Text = txtSRUnitNo.Text;
                txtStreetName.Text = txtSRStreetName.Text;
                ddlCountry.Text = ddlSRCountry.Text;
                ddlCountry_SelectedIndexChanged(sender, e);
                if (ddlDistrict.Items.Count > 0)
                {
                    ddlDistrict.Text = ddlSRDistrict.Text;
                    ddlDistrict_SelectedIndexChanged(sender, e);
                    if (ddlCity.Items.Count > 0)
                    {
                        ddlCity.Text = ddlSRCity.Text;
                    }
                }

                txtPostCode.Text = txtSRPostCode.Text;
            }

            txtUnitNo.Enabled = !chkSameAsResident.Checked;
            txtStreetName.Enabled = !chkSameAsResident.Checked;
            ddlCountry.Enabled = !chkSameAsResident.Checked;
            ddlDistrict.Enabled = !chkSameAsResident.Checked;
            ddlCity.Enabled = !chkSameAsResident.Checked;
            txtPostCode.Enabled = !chkSameAsResident.Checked;

            rfvUnitNo.Enabled = !chkSameAsResident.Checked;
            rfvStreetName.Enabled = !chkSameAsResident.Checked;
            rfvCountry.Enabled = !chkSameAsResident.Checked;
            rfvDistrict.Enabled = !chkSameAsResident.Checked;
            rfvCity.Enabled = !chkSameAsResident.Checked;
            rfvPostCode.Enabled = !chkSameAsResident.Checked;

            if (chkSameAsResident.Checked)
            {
                chkSameAsResident.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                chkSameAsResident.ForeColor = System.Drawing.Color.Green;
            }
        }

    #endregion

    #region"Methods"  

        private void LoadFamilyMemberList(int _id)
        {
            FamilyMemberRelationshipTableList _listFamilyMemberRelation = new FamilyMemberRelationshipTableList();
            _listFamilyMemberRelation = FamilyMemberRelationshipTableDB.GetList(_id);
            gvFamilyMemberList.DataSource = _listFamilyMemberRelation;
            gvFamilyMemberList.DataBind();
        }
     
        private bool LoadTeacherInfo(int AccountID)
        {
            bool _retValue = true;
            UserAccountTable _account = new UserAccountTable();
            _account = UserAccountTableManager.GetItem(AccountID);

            if (_account == null)
            {
                lblTopMessage.Text = "Teacher account ID (" + AccountID.ToString() + ") is not found!";
                hplTeacher.Visible = false;
                _retValue = false;
            }
            else if (_account.UserAccountKey <= 0)
            {
                lblTopMessage.Text = "Teacher account ID is not found!";
                hplTeacher.Visible = false;
                _retValue = false;
            }

            if (_account != null && _account.UserAccountKey > 0)
            {
                if (_account.UserAccountTypeCode != 200)
                {
                    lblTopMessage.Text = "Only for Teacher account ID will be loaded.";
                    hplTeacher.Visible = false;
                    _retValue = false;
                }

                lblTeacherUserAccountKey.Text = _account.UserAccountKey.ToString();
                lblSchoolKey.Text = _account.SchoolKey.ToString();
                TeacherProfileTable _sprofile = new TeacherProfileTable();
                TeacherAddressTable _saddress = new TeacherAddressTable();
                TeacherContactTable _scontact = new TeacherContactTable();
                FamilyMemberRelationshipTable _gsrelation = new FamilyMemberRelationshipTable();

                _sprofile = TeacherProfileTableManager.GetItemByUserAccountKey(AccountID, Convert.ToInt32(lblSchoolKey.Text));
                if (_sprofile != null && _sprofile.TeacherProfileKey > 0)
                {
                    lblTeacherProfileKey.Text = _sprofile.TeacherProfileKey.ToString();
                    lblSchoolKey.Text = _sprofile.SchoolKey.ToString();
                    string _fullName = string.Empty;
                    if (_sprofile.FirstName.Length > 0)
                    {
                        _fullName = _sprofile.FirstName;
                    }

                    if (_sprofile.LastName.Length > 0)
                    {
                        _fullName += " " + _sprofile.LastName;
                    }

                    if (_sprofile.OtherName.Length > 0)
                    {
                        _fullName += " &lt;or&gt; " + _sprofile.OtherName;
                    }
               

                    DateTime _dtDOB = _sprofile.DateOfBirth;
                    int _age = DateTime.Now.Year - _dtDOB.Year;
                    _fullName += " (" + _sprofile.Gender + ", " + _age.ToString() + " yrs)";
                    lblTeacherFullName.Text = _fullName;
                    lblTeacherNRICNumber.Text = _sprofile.NRICNumber;

                
                }
                else
                {
                    _retValue = false;
                }

                _saddress = TeacherAddressTableManager.GetItemByUserAccountKey(AccountID, 1);
                if (_saddress != null && _saddress.TeacherAddressKey > 0)
                {
               
                    txtSRUnitNo.Text = _saddress.UnitNo;
                    txtSRStreetName.Text = _saddress.StreetName;
                    ddlSRCountry.Text = _saddress.CountryCode.ToString();
                    ddlSRCountry_SelectedIndexChanged(null, null);

                    ddlSRDistrict.Text = _saddress.DistrictCode.ToString();
                    ddlSRDistrict_SelectedIndexChanged(null, null);
                    ddlSRCity.Text = _saddress.CityCode.ToString();


                    txtSRPostCode.Text = _saddress.PostalCode;

                    lblTeacherAddressInfo.Text = txtSRUnitNo.Text + ", ";
                    lblTeacherAddressInfo.Text += txtSRStreetName.Text + ", ";
                    lblTeacherAddressInfo.Text += ddlSRCity.SelectedItem.Text + ", " + ddlSRDistrict.SelectedItem.Text + ", ";
                    lblTeacherAddressInfo.Text += ddlSRCountry.SelectedItem.Text;

                    divTeacherAddressInfo.Visible = false;
                    txtSRUnitNo.Enabled = false;
                    txtSRStreetName.Enabled = false;
                    ddlSRCity.Enabled = false;
                    ddlSRDistrict.Enabled = false;
                    ddlSRCountry.Enabled = false;
                    txtSRPostCode.Enabled = false;
                }
                else
                {
                    _retValue = false;
                }

                _scontact = TeacherContactTableManager.GetItemByUserAccountKey(AccountID);
                if (_scontact != null && _scontact.TeacherContactKey > 0)
                {
                    string _contactInfo = string.Empty;
                    if (_scontact.MobileNumber != null && _scontact.MobileNumber.Length > 0)
                    {
                        _contactInfo = "Mobile: " + _scontact.MobileNumber;
                    }

                    if (_scontact.HomePhoneNumber != null && _scontact.HomePhoneNumber.Length > 0)
                    {
                        if (_contactInfo.Length > 0)
                        {
                            _contactInfo += ", ";
                        }
                        _contactInfo += "Home: " + _scontact.HomePhoneNumber;
                    }

                    if (_scontact.OtherNumber != null && _scontact.OtherNumber.Length > 0)
                    {
                        if (_contactInfo.Length > 0)
                        {
                            _contactInfo += ", ";
                        }

                        _contactInfo += "Other: " + _scontact.OtherNumber;
                    }

                    if (_scontact.EmailAddress != null && _scontact.EmailAddress.Length > 0)
                    {
                        if (_contactInfo.Length > 0)
                        {
                            _contactInfo += ", ";
                        }

                        _contactInfo += "Email: " + _scontact.EmailAddress;
                    }

                    lblTeacherContactInfo.Text = _contactInfo;
                
                }
                else
                {
                    _retValue = false;
                }
            }

            return _retValue;
        }

        private void LoadFamilyMemberInfo(int RelationID,int  FamilyMemberUserAccountID)
        {
            UserAccountTable _account = new UserAccountTable();
            _account = UserAccountTableManager.GetItem(FamilyMemberUserAccountID);

            if (_account == null)
            {
                lblTopMessage.Text = "Family Member account ID (" + FamilyMemberUserAccountID.ToString() + ") is not found!";
            }
            else if (_account.UserAccountKey <= 0)
            {
                lblTopMessage.Text = "Family Member account ID is not found!";
            }

            if (_account != null && _account.UserAccountKey > 0)
            {
                if (_account.UserAccountTypeCode != 700)
                {
                    lblTopMessage.Text = "Only for Family Member account ID will be loaded.";
                    return;
                }

                lblFamilyMemberAccountKey.Text = _account.UserAccountKey.ToString();

                FamilyMemberProfileTable _profile = new FamilyMemberProfileTable();

                FamilyMemberAddressTable _address = new FamilyMemberAddressTable();

                FamilyMemberContactTable _contact = new FamilyMemberContactTable();

                FamilyMemberRelationshipTable _gsrelation = new FamilyMemberRelationshipTable();

                _profile = FamilyMemberProfileTableManager.GetItemByUserAccountKey(FamilyMemberUserAccountID);
                if (_profile != null && _profile.FamilyMemberProfileKey > 0)
                {
                    lblFamilyMemberAccountKey.Text = _profile.FamilyMemberProfileKey.ToString();
                    txtFirstName.Text = _profile.FirstName;
                    txtLastName.Text = _profile.LastName;
                    txtOtherName.Text = _profile.OtherName;
                    ddlGender.SelectedValue = _profile.Gender;

                    DateTime _dtDOB = _profile.DateOfBirth;
                    ddlDOBDay.Text = _dtDOB.Day.ToString("00");
                    ddlDOBMonth.Text = _dtDOB.Month.ToString("00");
                    ddlDOBYear.Text = _dtDOB.Year.ToString("0000");

                    txtNRICNumber.Text = _profile.NRICNumber;             
               

                    ddlDrivingLicenceType.Text = _profile.DrivingLicenceType.ToString();
                    txtDrivingLicenceNumber.Text = _profile.DrivingLicenseNumber;

                    ddlTravelDocType.Text = _profile.TravelDocumentType.ToString();
                    txtTravelDocNumber.Text = _profile.TravelDocumentNumber;

                    ddlBloodGroup.Text = _profile.BloodGroupCode.ToString();
                    ddlMaritalStatus.Text = _profile.MartialStatusCode.ToString();
                    ddlRaceCode.Text = _profile.RaceCode.ToString();
                    ddlReligion.Text = _profile.ReligionCode.ToString();
                    ddlNationality.Text = _profile.NationalityCode.ToString();
                    txtWebsiteUrl.Text = _profile.WebSiteUrl;
                 
                }

                //Resident Address
                _address = FamilyMemberAddressTableManager.GetItemByUserAccountKey(_profile.UserAccountKey);
                if (_address != null && _address.FamilyMemberAddressKey > 0)
                {
                    lblFamilyMemberPAddressKey.Text = _address.FamilyMemberAddressKey.ToString();
                 

                    if (chkSameAsResident.Checked == false)
                    {
                        txtUnitNo.Text = _address.UnitNo;
                        txtStreetName.Text = _address.StreetName;
                        ddlCountry.Text = _address.CountryCode.ToString();
                        ddlCountry_SelectedIndexChanged(null, null);

                        ddlDistrict.Text = _address.DistrictCode.ToString();
                        ddlDistrict_SelectedIndexChanged(null, null);

                        ddlCity.Text = _address.CityCode.ToString();

                        txtPostCode.Text = _address.PostalCode;
                    }
                }

                _contact = FamilyMemberContactTableManager.GetItemByUserAccountKey(_profile.UserAccountKey);
                if (_contact != null && _contact.FamilyMemberContactKey > 0)
                {
                    lblFamilyMemberContactKey.Text = _contact.FamilyMemberContactKey.ToString();
                    txtMobileNumber.Text = _contact.MobileNumber;
                    txtHomePhoneNumber.Text = _contact.HomePhoneNumber;
                    txtFaxNumber.Text = _contact.FaxNumber;
                    txtOfficePhoneNumber.Text = _contact.OfficePhoneNumber;
                    txtEmailAddress.Text = _contact.EmailAddress;
                    txtOtherContactNumber.Text = _contact.OtherNumber;
                }

                _gsrelation = FamilyMemberRelationshipTableManager.GetItem(RelationID);
                if (_gsrelation != null && _gsrelation.RelationshipID > 0)
                {
                    lblTeacherFamilyMemberRelationTypeKey.Text = _gsrelation.RelationshipID.ToString();
                    ddlGSRelation.Text = _gsrelation.RelationshipTypeCode.ToString();                 
                }
             
            }

        }        

        private bool CheckValidation()
        {
            bool _retValue = true;

            //csvContactNumbers.Validate();        
            csvDOB.Validate();         

            ChangeServerSideValidationEffect();
            lblTopMessage.Text = lblMessage.Text;
            return _retValue;
        }       

        private void ChangeServerSideValidationEffect()
        {

       

            if (csvDOB.IsValid)
            {
                ddlDOBDay.ControlStyle.CssClass = "";
                ddlDOBMonth.ControlStyle.CssClass = "";
                ddlDOBYear.ControlStyle.CssClass = "";
            }
            else
            {
                ddlDOBDay.ControlStyle.CssClass = "validationError";
                ddlDOBMonth.ControlStyle.CssClass = "validationError";
                ddlDOBYear.ControlStyle.CssClass = "validationError";
            }

        }        

        protected void ClearFamilyMemberInfo()
        {
            ddlGSRelation.SelectedIndex = 0;
            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            txtOtherName.Text = string.Empty;
            ddlGender.SelectedIndex = 0;
            ddlDOBYear.SelectedIndex = 0;
            ddlDOBMonth.SelectedIndex = 0;
            ddlDOBDay.SelectedIndex = 0;
            txtNRICNumber.Text = string.Empty;
            ddlBloodGroup.SelectedIndex = 0;
            ddlMaritalStatus.SelectedIndex = 0;
            ddlRaceCode.SelectedIndex = 0;
            ddlReligion.SelectedIndex = 0;
            ddlNationality.SelectedIndex = 0;
            ddlTravelDocType.SelectedIndex = 0;
            ddlDrivingLicenceType.SelectedIndex = 0;
            ddlGSRelation.SelectedIndex = 0;
            ddlSRCountry.SelectedIndex = 0 ;
            ddlCountry.SelectedIndex = 0;
            txtUnitNo.Text = string.Empty;
            txtStreetName.Text = string.Empty;
            txtMobileNumber.Text = string.Empty;
            txtHomePhoneNumber.Text = string.Empty;
            txtOfficePhoneNumber.Text = string.Empty;
            txtFaxNumber.Text = string.Empty;
            txtOtherContactNumber.Text = string.Empty;
            txtEmailAddress.Text = string.Empty;
            txtWebsiteUrl.Text = string.Empty;
            txtPostCode.Text = string.Empty;
            txtSRPostCode.Text = string.Empty;
            txtTravelDocNumber.Text = string.Empty;
            txtDrivingLicenceNumber.Text = string.Empty;
            ddlDistrict.SelectedIndex = -1;
            if (ddlCity.SelectedItem != null)
            {
                ddlCity.SelectedItem.Text = string.Empty;
            }

        }

    #endregion

}